from fastapi import FastAPI
from contextlib import asynccontextmanager
from app.api.router import router as api_router
from typing import AsyncGenerator
from app.core.db import init_mysql


# 使用 lifespan 管理应用生命周期
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
    print("应用启动完成")
    # 应用启动时初始化 MySQL 数据库连接
    async with init_mysql(app):
        yield
    # 应用关闭时 MySQL 连接会自动关闭
    print("应用关闭")


app = FastAPI(lifespan=lifespan)

app.include_router(api_router)
